package com.huanbao.oil.app.activity;

import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


import com.huanbao.oil.app.R;
import com.huanbao.oil.app.api.ApiConfig;
import com.huanbao.oil.app.api.IPPortModel;
import com.huanbao.oil.app.commons.SharedPreferencesUtils;
import com.huanbao.oil.app.commons.UtilsLog;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class SettingActivity extends AppCompatActivity implements ApiConfig {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.ac_setting_et_ip)
    EditText acSettingEtIp;
    @BindView(R.id.ac_setting_et_port)
    EditText acSettingEtPort;
    @BindView(R.id.ac_set_btn_cancle2222)
    Button acSetBtnCancle2222;
    @BindView(R.id.ac_set_view_m)
    Button acSetViewM;
    @BindView(R.id.ac_set_btn_save2222)
    Button acSetBtnSave2222;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_setting);
        ButterKnife.bind(this);

        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);//左侧添加一个默认的返回图标
        getSupportActionBar().setHomeButtonEnabled(true); //设置返回键可用

    }

    @Override
    protected void onResume() {
        super.onResume();
        if (SharedPreferencesUtils.getIPModel(this) != null) {
            if (!TextUtils.isEmpty(SharedPreferencesUtils.getIPModel(this).getIp()
            ) && !TextUtils.isEmpty(SharedPreferencesUtils.getIPModel(this).getIp_port())
            ) {

                String netIp = SharedPreferencesUtils.getIPModel(this).getIp();
                String duanKou = SharedPreferencesUtils.getIPModel(this).getIp_port();
                acSettingEtIp.setText(netIp);
                acSettingEtPort.setText(duanKou);
            } else {
//                Toast.makeText(this, "请设置IP端口号", Toast.LENGTH_LONG).show();
            }
        } else {
//            Toast.makeText(this, "请设置IP端口号", Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
//        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        } else if (id == R.id.action_help) {

        } else if (id == android.R.id.home) {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }

    @OnClick({R.id.ac_set_btn_cancle2222, R.id.ac_set_btn_save2222,R.id.ac_set_view_m})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ac_set_btn_cancle2222:
                finish();
                break;
            case R.id.ac_set_view_m:
                Intent intent =  new Intent(Settings.ACTION_SETTINGS);
                startActivity(intent);
//                Toast.makeText(this, "系统设置成功", Toast.LENGTH_SHORT).show();
                break;
            case R.id.ac_set_btn_save2222:
                String strIp = null;//ip
                String strPort = null;//端口号
                strIp = acSettingEtIp.getText().toString().trim();
                strPort = acSettingEtPort.getText().toString().trim();
                if (!TextUtils.isEmpty(strIp) && !TextUtils.isEmpty(strPort)) {
                    String ip_port = BASE_URL_front + strIp + ":" + strPort;
                    SharedPreferencesUtils.setHuanBaoIP(this, ip_port);//一般请求接口的ip
                    IPPortModel model = new IPPortModel();
                    model.setIp(strIp);
                    model.setIp_port(strPort);
                    SharedPreferencesUtils.setIPModel(this, model);
                    UtilsLog.e(model.toString());
                    Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(this, "IP或端口号不能为空", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }
}
